package cn.com.cfae.iras.doc.analyze.components.integrity;


import cn.com.cfae.iras.doc.analyze.beans.web.Detail;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.handUtil.SearchKeywordUtil;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 架构完整性
 *
 * 检查关键词主营业务、（营业收入、营业成本、毛利、毛利率、占比、三年及一期）是否存在，不存在提示缺失(拆成关键词)；
 *
 *否引用20xx年第xx期xx品种募集说明书
 *
 * 预评/发行阶段
 *
 */
@Component("I1026")
@Scope("prototype")
public class I1026 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1026.class);
    @Autowired
    private HandingData handingData;

    @Autowired
    private SearchKeywordUtil searchKeywordUtil;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1026组件M");
        List<ComponentResult> componentResults = checkRulesI1026(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1026组件BM");
        List<ComponentResult> componentResults = checkRulesI1026(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取I1026组件DM");
        List<ComponentResult> componentResults = checkRulesI1026(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取I1026组件BDM");
        List<ComponentResult> componentResults = checkRulesI1026(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }


    private List<ComponentResult> checkRulesI1026(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;

        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean flag = false;
        String[] keywords = new String[]{"营业收入","营业成本","毛利","毛利率","占比","三年及一期"};
        int startPage = 0;

        try {

        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(model);
            startPage = chapterModel!=null?chapterModel.getStartPage():0;
            if (chapterModel!=null){
                //先判断表是否存在  营业收入、营业成本、毛利、毛利率、占比、三年及一期
                List<PageModel> pageModelList = chapterModel.getPageModelList();
                //营业收入
                boolean yysrIsExest = false;
                int yysrPageNum = startPage;
                List<ComponentResult.TextPosition> yysrContentAndPositionList = null;
                TextLayout yysrHead = chapterModel.getModel("YYSR").getTableNameTextLayout();
                if (yysrHead==null||StringUtils.isEmpty(yysrHead.getText())){

                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel :pageModelList){
                            //Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), "营业收入");
                            Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), "营业收入,主营业务收入",null);

                            flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (flag){
                                yysrPageNum = pageModel.getPageNumber();
                                yysrPageNum = yysrPageNum!=0?yysrPageNum:startPage;
                                yysrContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }
                        if (flag){
                            yysrIsExest = true;
                        }
                    }
                }else {
                    yysrContentAndPositionList= new ArrayList<>();
                    yysrPageNum = yysrHead.getPageNumber();
                    yysrPageNum = yysrPageNum!=0?yysrPageNum:startPage;
                    ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(yysrHead.getLeft());
                    contentAndPosition.setY(yysrHead.getTop());
                    contentAndPosition.setContent(yysrHead.getText());
                    yysrContentAndPositionList.add(contentAndPosition);
                    yysrIsExest = true;
                }

                if (!yysrIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-营业收入","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }else {

                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-营业收入","正确",false,getRegulationId(), yysrPageNum,yysrContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }


                //营业成本
                TextLayout yycbHead = chapterModel.getModel("YYCB").getTableNameTextLayout();

                int yycbPageNum = startPage;
                List<ComponentResult.TextPosition> yycbContentAndPositionList = null;
                boolean yycbIsExest = false;
                if (yycbHead==null||StringUtils.isEmpty(yycbHead.getText())){
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel :pageModelList){
                            //Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), "营业成本");
                            Map<String, Object> containsKeyWordOfRegex2Lines =searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), "营业成本,主营业务成本",null);
                            flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (flag){
                                yycbPageNum = pageModel.getPageNumber();
                                yycbPageNum = yycbPageNum!=0?yycbPageNum:startPage;
                                yycbContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }
                        if (flag){
                            yycbIsExest = true;
                        }
                    }
                }else {
                    yycbContentAndPositionList = new ArrayList<>();
                    yycbPageNum = yycbHead.getPageNumber();
                    yycbPageNum = yycbPageNum!=0?yycbPageNum:startPage;
                    ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(yycbHead.getLeft());
                    contentAndPosition.setY(yycbHead.getTop());
                    contentAndPosition.setContent(yycbHead.getText());
                    yycbContentAndPositionList.add(contentAndPosition);
                    yycbIsExest = true;
                }


                if (!yycbIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-营业成本","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }else {
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-营业成本","正确",false,getRegulationId(), yycbPageNum,yycbContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }

                //毛利润

                TextLayout mlrHead = chapterModel.getModel("MLR").getTableNameTextLayout();
                int mlrPageNum = startPage;
                List<ComponentResult.TextPosition> mlrContentAndPositionList = null;
                boolean mlrIsExest = false;

                if (mlrHead==null||StringUtils.isEmpty(mlrHead.getText())){
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel :pageModelList){
                           // Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), "毛利润");
                            Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), "毛利润,主营业务毛利润,主营业务毛利",null);

                            flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (flag){
                                mlrPageNum = pageModel.getPageNumber();
                                mlrPageNum = mlrPageNum!=0?mlrPageNum:startPage;
                                mlrContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }
                        if (flag){
                            mlrIsExest = true;
                        }
                    }
                }else {
                    mlrContentAndPositionList = new ArrayList<>();
                    mlrPageNum = mlrHead.getPageNumber();
                    mlrPageNum = mlrPageNum!=0?mlrPageNum:startPage;
                    ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(mlrHead.getLeft());
                    contentAndPosition.setY(mlrHead.getTop());
                    contentAndPosition.setContent(mlrHead.getText());
                    mlrContentAndPositionList.add(contentAndPosition);
                    mlrIsExest = true;
                }

                if (!mlrIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-毛利润","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }else {

                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-毛利润","正确",false,getRegulationId(), mlrPageNum,mlrContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }


                //毛利率
                TextLayout mllHead = chapterModel.getModel("MLL").getTableNameTextLayout();
                int mllPageNum = startPage;
                List<ComponentResult.TextPosition> mllContentAndPositionList = null;
                boolean mllIsExest = false;
                if (mlrHead==null||StringUtils.isEmpty(mllHead.getText())){
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel :pageModelList){
                            //Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), "毛利率");
                            Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), "毛利率,毛利润率,主营业务毛利率",null);
                            flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (flag){
                                mllPageNum = pageModel.getPageNumber();
                                mllPageNum = mllPageNum!=0?mllPageNum:startPage;
                                mllContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }
                        if (flag){
                            mllIsExest = true;
                        }
                    }
                }else {
                    mllContentAndPositionList = new ArrayList<>();
                    mllPageNum = mllHead.getPageNumber();
                    mllPageNum = mllPageNum!=0?mllPageNum:startPage;
                    ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(mllHead.getLeft());
                    contentAndPosition.setY(mllHead.getTop());
                    contentAndPosition.setContent(mllHead.getText());
                    mllContentAndPositionList.add(contentAndPosition);
                    mllIsExest = true;
                }
                if (!mllIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-毛利率","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }else {
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-毛利率","正确",false,getRegulationId(), mllPageNum,mllContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }


                //占比

                Map<String, List<Map<String, ExtractItem>>> yysr = chapterModel.getModel("YYSR").getTables2();
                int zbPageNum = startPage;
                List<ComponentResult.TextPosition> zbContentAndPositionList = null;
                boolean zbIsExest = false;
                if (isEmptyOfMap(yysr)){
                    Map<String, List<Map<String, ExtractItem>>> yycb = chapterModel.getModel("YYCB").getTables2();
                    if (isEmptyOfMap(yycb)){
                        Map<String, List<Map<String, ExtractItem>>> mlr = chapterModel.getModel("MLR").getTables2();
                        if (isEmptyOfMap(mlr)){
                            if (CollectionUtils.isNotEmpty(pageModelList)){
                                for (PageModel pageModel :pageModelList){
                                   // Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), "占比");
                                    Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), "占比",null);
                                    flag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                                    if (flag){
                                        zbPageNum = pageModel.getPageNumber();
                                        zbPageNum = zbPageNum!=0?zbPageNum:startPage;
                                        zbContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                        break;
                                    }
                                }
                                if (flag){
                                    zbIsExest = true;
                                }
                            }
                        }else {
                            Boolean zbFlag = getZb(mlr);
                            if (zbFlag){
                                Map<String, Object> mlrMap = getPageAndPosition(mlr, "ZB",startPage);
                                zbPageNum = (int)mlrMap.get("pageNum");
                                zbPageNum = zbPageNum!=0?zbPageNum:startPage;
                                zbContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");
                                zbIsExest = true;
                            }
                        }
                    }else {
                        Boolean zbFlag = getZb(yycb);
                        if (zbFlag){
                            Map<String, Object> mlrMap = getPageAndPosition(yycb, "ZB",startPage);
                            zbPageNum = (int)mlrMap.get("pageNum");
                            zbPageNum = zbPageNum!=0?zbPageNum:startPage;
                            zbContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");
                            zbIsExest = true;
                        }


                    }
                }else {
                    Boolean zbFlag = getZb(yysr);
                    if (zbFlag){
                        Map<String, Object> mlrMap = getPageAndPosition(yysr, "ZB",startPage);
                        zbPageNum = (int)mlrMap.get("pageNum");
                        zbPageNum = zbPageNum!=0?zbPageNum:startPage;
                        zbContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");
                        zbIsExest = true;
                    }
                }

                if (!zbIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-占比","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }else {
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-占比","正确",false,getRegulationId(), zbPageNum,zbContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }

                //三年及一期

                Map<String, List<Map<String, ExtractItem>>> snjyqYysr = chapterModel.getModel("YYSR").getTables2();
                int snjyqPageNum = startPage;
                List<ComponentResult.TextPosition> snjyqContentAndPositionList = null;
                boolean snjyqIsExest = false;
                if (isEmptyOfMap(snjyqYysr)){
                    Map<String, List<Map<String, ExtractItem>>> yycb = chapterModel.getModel("YYCB").getTables2();
                    if (isEmptyOfMap(yycb)){
                        Map<String, List<Map<String, ExtractItem>>> mlr = chapterModel.getModel("MLR").getTables2();
                        if (!isEmptyOfMap(mlr)){
                            Boolean  qxFlag = getQx(mlr);
                            if (qxFlag){
                                /*Map<String, Object> mlrMap = getPageAndPosition(mlr, "YYSR",startPage);
                                snjyqPageNum = (int)mlrMap.get("pageNum");
                                snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                                snjyqContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");*/
                                snjyqContentAndPositionList = new ArrayList<>();
                                snjyqPageNum = mllHead.getPageNumber();
                                snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                                ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                                contentAndPosition.setX(mllHead.getLeft());
                                contentAndPosition.setY(mllHead.getTop());
                                contentAndPosition.setContent(mllHead.getText());
                                snjyqContentAndPositionList.add(contentAndPosition);


                                snjyqIsExest = true;
                            }
                        }
                    }else {
                        Boolean  qxFlag = getQx(yycb);
                        if (qxFlag){
                            /*Map<String, Object> mlrMap = getPageAndPosition(yycb, "YYSR",startPage);
                            snjyqPageNum = (int)mlrMap.get("pageNum");
                            snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                            snjyqContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");*/

                            snjyqContentAndPositionList = new ArrayList<>();
                            snjyqPageNum = yycbHead.getPageNumber();
                            snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                            ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                            contentAndPosition.setX(yycbHead.getLeft());
                            contentAndPosition.setY(yycbHead.getTop());
                            contentAndPosition.setContent(yycbHead.getText());
                            snjyqContentAndPositionList.add(contentAndPosition);

                            snjyqIsExest = true;
                        }


                    }
                }else {
                    Boolean  qxFlag = getQx(yysr);
                    if (qxFlag){

                        /*Map<String, Object> mlrMap = getPageAndPosition(yysr, "YYSR",startPage);
                        snjyqPageNum = (int)mlrMap.get("pageNum");
                        snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                        snjyqContentAndPositionList = (List<ComponentResult.TextPosition> )mlrMap.get("contentAndPositionList");*/
                        snjyqContentAndPositionList= new ArrayList<>();
                        snjyqPageNum = yysrHead.getPageNumber();
                        snjyqPageNum = snjyqPageNum!=0?snjyqPageNum:startPage;
                        ComponentResult.TextPosition contentAndPosition = new ComponentResult().new TextPosition();
                        contentAndPosition.setX(yysrHead.getLeft());
                        contentAndPosition.setY(yysrHead.getTop());
                        contentAndPosition.setContent(yysrHead.getText());
                        snjyqContentAndPositionList.add(contentAndPosition);

                        snjyqIsExest = true;
                    }
                }

                if (snjyqIsExest){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-三年及一期","正确",false,getRegulationId(), snjyqPageNum,snjyqContentAndPositionList,"01");
                    componentResults.add(componentResult);
                }else {
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-三年及一期","缺失",true,getRegulationId(), startPage,null,"02");
                    componentResults.add(componentResult);
                }

            }
        }
            if (CollectionUtils.isEmpty(componentResults)){
                for (String key:keywords){
                    componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-"+key,"未识别,建议人工核对",true,getRegulationId(), startPage,null,"03");
                    componentResults.add(componentResult);
                }
            }
        }catch (Exception e){
            componentResults.clear();
            for (String key:keywords){
                componentResult =   handingData.getComponentResult(getCatalogId(),getChapterId(),ruleName,"主营业务关键词检查-"+key,"未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }
            log.info(e.getMessage(),e);
        }
        return  componentResults;
    }

    private Map<String,Object> getPageAndPosition(Map<String, List<Map<String, ExtractItem>>> maps,String type,int startPage){
        Map<String,Object> reusltMap = new HashMap<>();
        int pageNum = startPage;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        for (String key :maps.keySet()){
            if (CollectionUtils.isEmpty(contentAndPositionList)){
                List<Map<String, ExtractItem>> list = maps.get(key);
                if (CollectionUtils.isNotEmpty(list)){
                    Map<String, ExtractItem> map = list.get(0);
                    if (!map.isEmpty()){
                        ExtractItem extractItem = map.get(type);
                        if (extractItem!=null){
                            KVText value = extractItem.getValue();
                            pageNum = value!=null?value.getPageNumber():0;
                            contentAndPositionList =  value!=null?getContentAndPositionList(value):null;
                            reusltMap.put("pageNum",pageNum);
                            reusltMap.put("contentAndPositionList",contentAndPositionList);
                        }
                    }
                }
            }else {
                break;
            }
        }
        if (reusltMap==null||reusltMap.isEmpty()){
            reusltMap.put("pageNum",pageNum);
            reusltMap.put("contentAndPositionList",contentAndPositionList);
        }
        return reusltMap;
    }

    private Boolean getZb(Map<String, List<Map<String, ExtractItem>>> table){
        boolean  flag = false;
        if (table==null||table.isEmpty()){
            return flag;
        }
        for (String key :table.keySet()){
            List<Map<String, ExtractItem>> list = table.get(key);
            if (CollectionUtils.isNotEmpty(list)){
                Map<String, ExtractItem> map = list.get(0);
                if (!map.isEmpty()){
                    ExtractItem extractItem = map.get("ZB");
                    if (extractItem!=null){
                        flag = true;
                    }
                }
            }
            break;
        }
        return flag;
    }

   private Boolean getQx(Map<String, List<Map<String, ExtractItem>>> table){
       boolean  flag = false;
       if (table.isEmpty()){
           return false;
       }
       int num = 0;
       for (String key :table.keySet()){
        num++;
       }
       if (num==4){
          flag = true;
       }
       return flag;
   }

    private Boolean isEmptyOfMap(Map<String, List<Map<String, ExtractItem>>> table ){
        if (table==null||table.isEmpty()){
            return true;
        }else {
            for (String key :table.keySet()){
                List<Map<String, ExtractItem>> list = table.get(key);
                if (CollectionUtils.isEmpty(list)){
                    return true;
                }
                break;
            }
        }
        return false;
    }

    /*public Map<String,Object> isContainsKeyWord(JsonElement jsonElement, String keyword, PageModel pageModel){
        Map<String, Object> reslt = new HashMap<>();

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){


                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行

                                        if (!isInTable(deJsonObject,pageModel)){
                                            Set deSet = deJsonObject.entrySet();
                                            Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                            Double x = 0.0;
                                            Double y = 0.0;
                                            while (deIt.hasNext()){
                                                Map.Entry<String, JsonElement> deEntry = deIt.next();
                                                String deKey= deEntry.getKey();
                                                if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                    y = Double.parseDouble(deEntry.getValue().getAsString());
                                                }
                                                if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                    x =  Double.parseDouble(deEntry.getValue().getAsString());
                                                }
                                                if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                    boolean isExist = false;
                                                    String count = deEntry.getValue().getAsString();
                                                    if (StringUtils.isNotEmpty(count)){

                                                        //前一行
                                                        ComponentResult.TextPosition frontTextPostion = getFrontString(dataJsonArray, d);

                                                        //当前行

                                                        //后一行
                                                        ComponentResult.TextPosition behindTextPostion = getBehindString(dataJsonArray, d);

                                                        //判断是否在当前行
                                                        if (!isExist&&count.contains(keyword)){
                                                            contentAndPosition = new ComponentResult().new TextPosition();
                                                            flag = true;
                                                            isExist = true;
                                                            int countBegenIndex = count.indexOf(keyword)+1;
                                                            int countEndIndex = countBegenIndex+keyword.length()-1;
                                                            contentAndPosition.setX(x);
                                                            contentAndPosition.setY(y);
                                                            contentAndPosition.setContent(count.substring(countBegenIndex-1,countEndIndex));
                                                            textPositions.add(contentAndPosition);
                                                            reslt.put("flag",flag);
                                                            reslt.put("position",textPositions);
                                                        }

                                                        //判断是否在后一行
                                                        if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                            String behindText = behindTextPostion.getContent();
                                                            if (behindText.contains(keyword)){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                flag = true;
                                                                isExist = true;
                                                                int countBegenIndex = behindText.indexOf(keyword)+1;
                                                                int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                contentAndPosition.setX(behindTextPostion.getX());
                                                                contentAndPosition.setY(behindTextPostion.getY());
                                                                contentAndPosition.setContent(behindText.substring(countBegenIndex-1,countEndIndex));
                                                                textPositions.add(contentAndPosition);
                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);
                                                            }
                                                        }
                                                        //判断前一行 和当前行
                                                        if (!isExist&&StringUtils.isNotEmpty(frontTextPostion.getContent())){

                                                            //前一行文本
                                                            String fornText = frontTextPostion.getContent();
                                                            //总文本
                                                            String temp = fornText+count;
                                                            if (temp.contains(keyword)){
                                                                flag = true;
                                                                isExist = true;
                                                                //当前关键词的位置
                                                                int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                //关键词长度
                                                                int keywrodLenth = keyword.length();
                                                                //关键词结尾位置
                                                                int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                //前一行长度
                                                                int frontLenth = fornText.length();
                                                                //第二行结束位置
                                                                int endIndex = keywordIndexEndLenth-countKeywrodIndex-1;

                                                                if (frontLenth>=countKeywrodIndex){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    contentAndPosition.setX(frontTextPostion.getX());
                                                                    contentAndPosition.setY(frontTextPostion.getY());
                                                                    contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                    textPositions.add(contentAndPosition);
                                                                }
                                                                if (endIndex<=count.length()){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    contentAndPosition.setX(x);
                                                                    contentAndPosition.setY(y);
                                                                    contentAndPosition.setContent(count.substring(0,endIndex));
                                                                    textPositions.add(contentAndPosition);
                                                                }
                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);
                                                            }
                                                        }
                                                        //判断当前行和后一行
                                                        if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                            //后一行的文本
                                                            String behindText = behindTextPostion.getContent();
                                                            //总文本
                                                            String temp = count+behindText;

                                                            if (temp.contains(keyword)){
                                                                flag = true;
                                                                isExist = true;

                                                                //当前关键词的位置
                                                                int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                //关键词长度
                                                                int keywrodLenth = keyword.length();
                                                                //关键词结尾位置
                                                                int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                //当前行长度
                                                                int coumtLenth = count.length();
                                                                //第二行结束位置
                                                                int endIndex = keywordIndexEndLenth-coumtLenth;

                                                                if (coumtLenth>=countKeywrodIndex){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    contentAndPosition.setX(x);
                                                                    contentAndPosition.setY(y);
                                                                    contentAndPosition.setContent(count.substring(countKeywrodIndex-1,coumtLenth));
                                                                    textPositions.add(contentAndPosition);
                                                                }
                                                                if (endIndex<=behindText.length()){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    contentAndPosition.setX(behindTextPostion.getX());
                                                                    contentAndPosition.setY(behindTextPostion.getY());
                                                                    contentAndPosition.setContent(behindText.substring(0,endIndex));
                                                                    textPositions.add(contentAndPosition);
                                                                }

                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);

                                                            }

                                                        }

                                                    }

                                                }
                                            }
                                        }

                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }

    //前一行
    private  ComponentResult.TextPosition  getFrontString(JsonArray dataJsonArray,int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row!=0){
            delmJsonArray = dataJsonArray.get(row-1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }

    //后一行
    private ComponentResult.TextPosition getBehindString(JsonArray dataJsonArray,int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row<=dataJsonArray.size()-2) {
            delmJsonArray = dataJsonArray.get(row + 1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();

                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }*/

}
